<template>
  <div id="detail">
    <!-- 导航栏 -->
    <nav-bar class="nav-color">
      <!-- 使用具名插槽替换navbar的内容 -->
      <div slot="left">
        <img
          @click="backClick"
          src="../../../assets/img/common/back.svg"
          alt="返回按钮"
          class="nav-btn"
        />
      </div>
      <div slot="center" class="nav-title">
        <div
          class="nav-title-item"
          v-for="(item, index) in title"
          :key="item.index"
          @click="titleClick(index)"
          :class="{ active: index === currentIndex }"
        >
          {{ item }}
        </div>
      </div>
    </nav-bar>
  </div>
</template>

<script>
import NavBar from "@/components/common/navbar/NavBar.vue"; //导入导航条组件

export default {
  name: "DetailNavBar",
  components: {
    NavBar,
  },
  data() {
    return {
      title: ["商品", "参数", "评论", "推荐"] /*标题参数 */,
      currentIndex: 0 /* 当前索引位置为0 */,
    };
  },
  methods: {
    // 点击当前标题切换颜色
    titleClick(index) {
      this.currentIndex = index;
    },
    // 返回功能实现
    backClick() {
      this.$router.back();
    },
  },
};
</script>
<style scoped>
/* 给父盒设置弹性布局 */
.nav-title {
  display: flex;
  flex-direction: row; /* 设置排列方式 */
  font-size: 12px;
}
/* 设置子盒子的样式 */
.nav-title-item {
  flex: 1; /* 据等分宽度 */
}
/* 点击选中的标题则切换颜色 */
.active {
  color: var(--color-high-text);
}

.nav-btn {
  padding-top: 10px;
  padding-left: 4px;
}
</style>